package com.yilin.tms.order.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.commons.exception.BusinessException;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.commons.util.DateTimeUtil;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.utils.OrderUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.commons.model.AuthData;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.commons.utils.UserUtil;
import com.yilin.tms.core.entity.order.dispatch.DispatchOrder;
import com.yilin.tms.core.entity.order.dispatch.DispatchOrderNote;
import com.yilin.tms.core.entity.order.dispatch.DispatchOrderNoteDetail;
import com.yilin.tms.core.entity.order.goods.GoodsOrder;
import com.yilin.tms.core.entity.order.goods.GoodsOrderNote;
import com.yilin.tms.core.entity.order.goods.GoodsOrderNoteDetail;
import com.yilin.tms.core.entity.order.party.OrderDispatchAutoConfig;
import com.yilin.tms.core.entity.order.party.OrderDispatchTask;
import com.yilin.tms.core.entity.order.rob.RobOrder;
import com.yilin.tms.core.entity.order.rob.RobOrderNote;
import com.yilin.tms.core.entity.order.rob.RobOrderNoteDetail;
import com.yilin.tms.core.entity.system.param.GoodsType;
import com.yilin.tms.core.entity.user.OrganType;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.core.remote.ISystemRemote;
import com.yilin.tms.core.remote.IUserRemote;
import com.yilin.tms.order.mapper.dispatch.DispatchOrderMapper;
import com.yilin.tms.order.mapper.dispatch.DispatchOrderNoteDetailMapper;
import com.yilin.tms.order.mapper.dispatch.DispatchOrderNoteMapper;
import com.yilin.tms.order.mapper.party.OrderDispatchAutoConfigMapper;
import com.yilin.tms.order.mapper.party.OrderDispatchTaskMapper;
import com.yilin.tms.order.service.order.IDispatchService;
import com.yilin.tms.order.service.order.IGoodsService;
import com.yilin.tms.order.service.order.IRobService;
import com.yilin.tms.order.service.owner.IGoodsOwnerService;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.*;

@Service
@Transactional(rollbackOn = Exception.class)
public class DispatchService extends ServiceImpl<DispatchOrderMapper, DispatchOrder> implements IDispatchService {

    @Resource
    DispatchOrderMapper dispatchOrderMapper;
    @Resource
    OrderDispatchTaskMapper orderDispatchTaskMapper;
    @Resource
    DispatchOrderNoteMapper dispatchOrderNoteMapper;
    @Resource
    DispatchOrderNoteDetailMapper dispatchOrderNoteDetailMapper;
    @Resource
    OrderDispatchAutoConfigMapper orderDispatchAutoConfigMapper;

    @Resource
    IGoodsService goodsService;
    @Resource
    IGoodsOwnerService goodsOwnerService;
    @Resource
    IRobService robService;

    @Resource
    IUserRemote userRemote;
    @Resource
    ISystemRemote systemRemote;


    @Override
    public PageData<DispatchOrder> getDispatchOrderPage(DispatchOrder query, Account loginUser, Integer page, Integer limit) {
        QueryWrapper<DispatchOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shipper_organize_id", loginUser.getOrganizeId());
        return dispatchOrderMapper.selectPage(new PageData<>(page, limit), QueryUtil.andQueryWrapper(queryWrapper, query));
    }

    @Override
    public DispatchOrder saveDispatchOrder(DispatchOrder dispatchOrder, Account loginUser) {
        //检查货主基本信息认证是否完善
        goodsOwnerService.checkGoodsRelease(dispatchOrder.transGoodsOrder());

        Organize shipperOrganize = ParamUtil.isSuccess(userRemote.getOrganizeById(loginUser.getOrganizeId()));
        Organize platformOrganize = ParamUtil.isSuccess(userRemote.getOrganizeById(shipperOrganize.getSuperiorPlatOrganizeId()));
        Organize zoneOrganize = ParamUtil.isSuccess(userRemote.getOrganizeById(platformOrganize.getSuperiorPlatOrganizeId()));
        //平台信息
//        dispatchOrder.setZonePlatId(zoneOrganize.getId());
//        dispatchOrder.setZonePlatName(zoneOrganize.getOrganizeName());
//        dispatchOrder.setPlatId(platformOrganize.getId());
//        dispatchOrder.setPlatName(platformOrganize.getOrganizeName());
        //基础信息
        dispatchOrder.setTitle(OrderUtil.genOrderTitle(ArrayUtil.first(dispatchOrder.getDispatchOrderNoteList())));
        dispatchOrder.setDispatchOrderNo(DateTimeUtil.dateNo("DS"));
        dispatchOrder.setDispatchStatus(DispatchOrder.DispatchStatus.going);
        dispatchOrder.setAddUserId(loginUser.getId());
        //货主信息
        dispatchOrder.setShipperOrganizeId(shipperOrganize.getId());
        dispatchOrder.setShipperOrganizeName(shipperOrganize.getOrganizeName());
        dispatchOrder.setShipperGroupOrganizeId(shipperOrganize.getGroupOrganizeId());
        dispatchOrder.setShipperGroupOrganizeName(shipperOrganize.getGroupOrganizeName());
        ///节点信息
        List<DispatchOrderNote> dispatchOrderNoteList = dispatchOrder.getDispatchOrderNoteList();
        //###检查发货节点、货物信息、货物量
        dispatchOrderNoteList.forEach(dispatchOrderNote -> {
            checkDispatchOrderNote(dispatchOrderNote);
            List<DispatchOrderNoteDetail> noteDetailList = dispatchOrderNote.getDispatchOrderNoteDetailList();
            ArrayUtil.forEach(noteDetailList, noteDetail -> checkDispatchInfo(dispatchOrder, noteDetail));
        });
        if (dispatchOrder.getId() == null) dispatchOrderMapper.insert(dispatchOrder);
        else dispatchOrderMapper.updateById(dispatchOrder);
        //保存节点货物信息
        saveNoteDetail(loginUser, dispatchOrder);
        return dispatchOrder;
    }

    @Override
    public OrderDispatchTask initiateDispatchOrder(String[] ids, Account loginUser) {
        List<DispatchOrder> dispatchOrderList;
        if (ids != null && ids.length > 0) {
            dispatchOrderList = dispatchOrderMapper.selectBatchIds(ArrayUtil.initList(ids));
        } else {
            QueryWrapper<DispatchOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("shipper_organize_id", loginUser.getOrganizeId());
            queryWrapper.eq("dispatch_status", DispatchOrder.DispatchStatus.going);
            dispatchOrderList = dispatchOrderMapper.selectList(queryWrapper);
        }
        List<AuthData.AuthItem> itemList = ArrayUtil.initList(AuthData.AuthItem.authIdCard);
        if (loginUser.getOrganType() != OrganType.individual) itemList.add(AuthData.AuthItem.authBusinessLicense);
        ParamUtil.isSuccess(userRemote.checkAuthStatus(loginUser.getId(), itemList.toArray(new AuthData.AuthItem[0])));
        List<String> listNos = ArrayUtil.fieldAdd(dispatchOrderList, DispatchOrder::getDispatchOrderNo);

        OrderDispatchTask orderDispatchTask = getGoingOrderDispatchTask(loginUser.getOrganizeId());
        if (orderDispatchTask != null) return orderDispatchTask;
        orderDispatchTask = new OrderDispatchTask();
        orderDispatchTask.setAlreadyDispatchCount(0);
        orderDispatchTask.setDispatchTaskStatus(OrderDispatchTask.DispatchTaskStatus.going);
        orderDispatchTask.setOrganizeId(loginUser.getOrganizeId());
        orderDispatchTask.setOrganizeName(loginUser.getOrganizeName());
        orderDispatchTask.setDispatchTime(new Date());
        orderDispatchTask.setAlreadyDispatchCount(0);
        orderDispatchTask.setTotalDispatchCount(listNos.size());
        orderDispatchTask.setFromAccountId(loginUser.getId());
        orderDispatchTask.setFromName(loginUser.getName());
        orderDispatchTask.setDispatchNos(String.join(",", listNos));
        orderDispatchTaskMapper.insert(orderDispatchTask);
        return orderDispatchTask;
    }

    //一键派单，就是将所有派单向转化到货单，并自动生成抢单
    @Override
    @Transactional(Transactional.TxType.NEVER)
    public ReturnData<Object> taskDispatchOrder(Account loginUser, DispatchOrder dispatchOrder) {
        try {
            GoodsOrder goodsOrder = dispatchOrder.transGoodsOrder();
            List<DispatchOrderNote> dispatchOrderNoteList = getDispatchOrderNoteListById(dispatchOrder.getId());
            List<GoodsOrderNote> goodsOrderNoteList = new ArrayList<>();
            dispatchOrderNoteList.forEach(dispatchOrderNote -> {
                GoodsOrderNote goodsOrderNote = dispatchOrderNote.transGoodsOrderNote();
                List<DispatchOrderNoteDetail> dispatchOrderNoteDetailList = getDispatchOrderNoteDetailListByNoteId(dispatchOrderNote.getId());
                List<GoodsOrderNoteDetail> goodsOrderNoteDetailList = new ArrayList<>();
                dispatchOrderNoteDetailList.forEach(dispatchOrderNoteDetail -> goodsOrderNoteDetailList.add(dispatchOrderNoteDetail.transGoodsOrderNoteDetail()));
                goodsOrderNote.setGoodsOrderNoteDetailList(goodsOrderNoteDetailList);
                goodsOrderNoteList.add(goodsOrderNote);
            });
            goodsOrder.setGoodsStatus(GoodsOrder.GoodsStatus.save);
            goodsOrder.setGoodsOrderNoteList(goodsOrderNoteList);
            goodsOrder.setShipperAccountId(loginUser.getId());
            goodsOrder.setDispatchOrderId(dispatchOrder.getId());
            goodsService.saveGoodsOrder(loginUser, goodsOrder);
            //开始派单 生成抢单
            RobOrder robOrder = genRobOrder(goodsOrder);
            Organize robOrganize = ParamUtil.isSuccess(userRemote.getOrganizeById(dispatchOrder.getTruckerOrganizeId()));
            Account robAccount = ParamUtil.isSuccess(userRemote.getAccountById(robOrganize.getManagerUserId()));
            robOrder.setRobOrganizeId(robOrganize.getId());
            robOrder.setRobRemark(dispatchOrder.getShipperOrganizeName() + "后台批量派单");
            robService.assignGoodsOder(loginUser, robOrder, goodsOrder, robOrganize, robAccount);
            //派单完成
            dispatchOrder.setDispatchCount(dispatchOrder.getDispatchCount() + 1);
            dispatchOrder.setLastDispatchTime(new Date());
            dispatchOrderMapper.updateById(dispatchOrder);
            return ReturnData.from(true, "派单完成");
        } catch (Exception e) {
            log.error("派单发生异常",e);
            return ReturnData.from(false, e.getMessage());
        }
    }

    @Override
    public void updateOrderDispatchTaskById(OrderDispatchTask orderDispatchTask) {
        orderDispatchTaskMapper.updateById(orderDispatchTask);
    }

    @Override
    public List<DispatchOrder> getDispatchOrderListByNo(List<String> nos) {
        QueryWrapper<DispatchOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dispatch_order_no", nos);
        queryWrapper.orderByAsc("dispatch_order_no");
        return dispatchOrderMapper.selectList(queryWrapper);
    }

    //将派单失败的任务所有货源+抢单全部作废
    @Override
    public void dispatchFailRestore(Account account, String dispatchOrderId) {
        List<GoodsOrder> orderList = goodsService.getGoodsOrderListByDispatchOrderId(dispatchOrderId);
        for (GoodsOrder goodsOrder : orderList) {
            goodsService.scrapGoodsOder(account, goodsOrder);
            List<RobOrder> robOrderList = robService.getRobSuccessListByGoodsOrderId(goodsOrder.getId());
            for (RobOrder robOrder : robOrderList) robOrder.setRobStatus(RobOrder.RobStatus.cancel);
            robService.updateBatchById(robOrderList);
        }
    }

    @Override
    public void updateDispatchAutoConfig(OrderDispatchAutoConfig autoConfig, Account loginUser) {
        autoConfig.setShipperOrganizeId(loginUser.getOrganizeId());
        autoConfig.setShipperOrganizeName(loginUser.getOrganizeName());
        if (autoConfig.getId() == null) {
            orderDispatchAutoConfigMapper.insert(autoConfig);
        } else {
            orderDispatchAutoConfigMapper.updateById(autoConfig);
        }
    }

    @Override
    public OrderDispatchAutoConfig getDispatchAutoConfig(Account loginUser) {
        QueryWrapper<OrderDispatchAutoConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shipper_organize_id", loginUser.getOrganizeId());
        return orderDispatchAutoConfigMapper.selectOne(queryWrapper);
    }

    @Override
    public List<OrderDispatchTask> getGoingOrderDispatchTaskList() {
        QueryWrapper<OrderDispatchTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dispatch_task_status", OrderDispatchTask.DispatchTaskStatus.going.index);
        return orderDispatchTaskMapper.selectList(queryWrapper);
    }

    @Override
    public OrderDispatchTask getGoingOrderDispatchTask(String shipperOrganizeId) {
        QueryWrapper<OrderDispatchTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dispatch_task_status", OrderDispatchTask.DispatchTaskStatus.going.index);
        queryWrapper.eq("organize_id", shipperOrganizeId);
        return orderDispatchTaskMapper.selectOne(queryWrapper);
    }

    @Override
    public List<OrderDispatchTask> getToDayOrderDispatchTask(String shipperOrganizeId) {
        QueryWrapper<OrderDispatchTask> queryWrapper = new QueryWrapper<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date start = calendar.getTime();
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date end = calendar.getTime();
        queryWrapper.between("create_time", start, end);
        queryWrapper.eq("organize_id", shipperOrganizeId);
        queryWrapper.orderByAsc("create_time");
        return orderDispatchTaskMapper.selectList(queryWrapper);
    }

    public List<DispatchOrderNote> getDispatchOrderNoteListById(String dispatchOrderId) {
        QueryWrapper<DispatchOrderNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dispatch_order_id", dispatchOrderId);
        return dispatchOrderNoteMapper.selectList(queryWrapper);
    }

    public List<DispatchOrderNoteDetail> getDispatchOrderNoteDetailListByNoteId(String dispatchOrderNoteId) {
        QueryWrapper<DispatchOrderNoteDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dispatch_order_note_id", dispatchOrderNoteId);
        return dispatchOrderNoteDetailMapper.selectList(queryWrapper);
    }

    //生成抢单
    @Override
    public RobOrder genRobOrder(GoodsOrder goodsOrder) {
        RobOrder robOrder = new RobOrder();
        robOrder.setGoodsOrderId(goodsOrder.getId());
        robOrder.setMeasureUnit(goodsOrder.getMeasureUnit());
        robOrder.setRobType(RobOrder.RobType.assign);
        List<RobOrderNote> robOrderNoteList = new ArrayList<>();
        goodsOrder.getGoodsOrderNoteList().forEach(goodsOrderNote -> {
            RobOrderNote robOrderNote = new RobOrderNote();
            robOrderNote.setGoodsOrderId(goodsOrder.getId());
            robOrderNote.setGoodsOrderNoteId(goodsOrderNote.getId());
            robOrderNote.setMeasureUnit(goodsOrderNote.getMeasureUnit());
            robOrderNote.setRobNoteMeasure(goodsOrderNote.getGoodsNoteMeasure());
            robOrderNote.setRobNoteTransportCost(goodsOrderNote.getGoodsNoteTransportCost());
            List<RobOrderNoteDetail> robOrderNoteDetailList = new ArrayList<>();
            goodsOrderNote.getGoodsOrderNoteDetailList().forEach(goodsOrderNoteDetail -> {
                RobOrderNoteDetail robOrderNoteDetail = new RobOrderNoteDetail();
                robOrderNoteDetail.setGoodsOrderId(goodsOrder.getId());
                robOrderNoteDetail.setGoodsNoteDetailId(goodsOrderNoteDetail.getId());
                robOrderNoteDetail.setRobMeasure(goodsOrderNoteDetail.getGoodsMeasure());
                robOrderNoteDetail.setMeasureUnit(goodsOrderNoteDetail.getMeasureUnit());
                robOrderNoteDetail.setRobTransportCost(goodsOrderNoteDetail.getGoodsTransportCost());
                robOrderNoteDetail.setRobUnitPrice(goodsOrderNoteDetail.getGoodsUnitPrice());
                robOrderNoteDetailList.add(robOrderNoteDetail);
            });
            robOrderNote.setRobOrderNoteDetailList(robOrderNoteDetailList);
            robOrderNoteList.add(robOrderNote);
        });
        robOrder.setRobOrderNoteList(robOrderNoteList);
        return robOrder;
    }

    @Override
    public DispatchOrder selectDispatchOrder(String id) {
        DispatchOrder dispatchOrder = dispatchOrderMapper.selectById(id);
        QueryWrapper<DispatchOrderNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dispatch_order_id", id);
        List<DispatchOrderNote> dispatchOrderNotes = dispatchOrderNoteMapper.selectList(queryWrapper);
        for (DispatchOrderNote dispatchOrderNote : dispatchOrderNotes) {
            List<DispatchOrderNoteDetail> dispatchOrderNoteDetailList = dispatchOrderNoteDetailMapper.selectList(
                    new QueryWrapper<DispatchOrderNoteDetail>().eq("dispatch_order_note_id", dispatchOrderNote.getId()));
            dispatchOrderNote.setDispatchOrderNoteDetailList(dispatchOrderNoteDetailList);
        }
        dispatchOrder.setDispatchOrderNoteList(dispatchOrderNotes);
        return dispatchOrder;
    }


    //### 内部保存节点信息
    private void saveNoteDetail(Account loginUser, DispatchOrder dispatchOrder) {
        List<DispatchOrderNote> dispatchOrderNoteList = dispatchOrder.getDispatchOrderNoteList();
        dispatchOrderNoteList.forEach(dispatchOrderNote -> {
            List<DispatchOrderNoteDetail> noteDetailList = dispatchOrderNote.getDispatchOrderNoteDetailList();
            dispatchOrderNote.setDispatchOrderId(dispatchOrder.getId());
//            dispatchOrderNote.setZonePlatId(dispatchOrder.getZonePlatId());
//            dispatchOrderNote.setZonePlatName(dispatchOrder.getZonePlatName());
//            dispatchOrderNote.setPlatId(dispatchOrder.getPlatId());
//            dispatchOrderNote.setPlatName(dispatchOrder.getPlatName());
            dispatchOrderNote.setAddUserId(loginUser.getId());
            dispatchOrderNote.setMeasureUnit(dispatchOrder.getMeasureUnit());
            dispatchOrderNote.setGoodsNoteTransportCost(ArrayUtil.numListAdd(noteDetailList, DispatchOrderNoteDetail::getGoodsTransportCost));
            dispatchOrderNote.setGoodsNoteMeasure(ArrayUtil.numListAdd(noteDetailList, DispatchOrderNoteDetail::getGoodsMeasure));
            if (dispatchOrderNote.getId() == null) dispatchOrderNoteMapper.insert(dispatchOrderNote);
            else dispatchOrderNoteMapper.updateById(dispatchOrderNote);
            ArrayUtil.forEach(noteDetailList, dispatchNoteDetail -> {
                GoodsType goodsType = ParamUtil.isSuccess(systemRemote.getGoodsTypeById(dispatchNoteDetail.getGoodsTypeId()));
                dispatchNoteDetail.setDispatchOrderId(dispatchOrder.getId());
                dispatchNoteDetail.setDispatchOrderNoteId(dispatchOrderNote.getId());
//                dispatchNoteDetail.setPlatId(dispatchNoteDetail.getPlatId());
//                dispatchNoteDetail.setPlatName(dispatchNoteDetail.getPlatName());
//                dispatchNoteDetail.setZonePlatId(dispatchNoteDetail.getZonePlatId());
//                dispatchNoteDetail.setZonePlatName(dispatchNoteDetail.getZonePlatName());
                dispatchNoteDetail.setAddUserId(loginUser.getId());
                dispatchNoteDetail.setGoodsTypeName(goodsType.getName());
                dispatchNoteDetail.setGoodsTypeCode(goodsType.getCode());
                if (dispatchNoteDetail.getId() == null) dispatchOrderNoteDetailMapper.insert(dispatchNoteDetail);
                else dispatchOrderNoteDetailMapper.updateById(dispatchNoteDetail);
            });
        });
        dispatchOrder.setUpdateUserId(loginUser.getId());
        dispatchOrder.setShipperTotalMeasure(ArrayUtil.numListAdd(dispatchOrderNoteList, DispatchOrderNote::getGoodsNoteMeasure));
        dispatchOrderMapper.updateById(dispatchOrder);
    }

    /**
     * 内部公共 判断发货节点基础信息
     */
    private void checkDispatchOrderNote(DispatchOrderNote dispatchOrderNote) {
        if (StringUtil.isEmpty(dispatchOrderNote.getConsignorName())) {
            throw new BusinessException("请添加发货联系人");
        }
        if (StringUtil.isEmpty(dispatchOrderNote.getConsignorPhone())) {
            throw new BusinessException("请添加发货联系人电话");
        }
        if (StringUtil.isEmpty(dispatchOrderNote.getConsignorCoordinate())) {
            throw new BusinessException("请添加发货地坐标");
        }
        if (StringUtil.isEmpty(dispatchOrderNote.getConsignorArea())) {
            throw new BusinessException("请添加发货地区");
        }
        if (StringUtil.isEmpty(dispatchOrderNote.getConsigneeName())) {
            throw new BusinessException("请添加收货联系人");
        }
        if (StringUtil.isEmpty(dispatchOrderNote.getConsigneePhone())) {
            throw new BusinessException("请添加收货联系人电话");
        }
        if (StringUtil.isEmpty(dispatchOrderNote.getConsigneeCoordinate())) {
            throw new BusinessException("请添加收货节地坐标");
        }
        if (StringUtil.isEmpty(dispatchOrderNote.getConsigneeArea())) {
            throw new BusinessException("请添加收货地区");
        }
    }

    /**
     * 内部公共 判断货物节点信息
     */
    private void checkDispatchInfo(DispatchOrder dispatchOrder, DispatchOrderNoteDetail dispatchOrderNoteDetail) {
        if (StringUtil.isEmpty(dispatchOrderNoteDetail.getMeasureUnit())) {
            throw new BusinessException("单位出错，请检查货物计量单位");
        }
        if (StringUtil.isEmpty(dispatchOrderNoteDetail.getGoodsTypeId())) {
            throw new BusinessException("请选择货物类型");
        }
        if (StringUtil.isEmpty(dispatchOrderNoteDetail.getGoodsName())) {
            throw new BusinessException("请输入货物名称");
        }
        if (dispatchOrderNoteDetail.getGoodsMeasure() == null || dispatchOrderNoteDetail.getGoodsMeasure() < 0) {
            throw new BusinessException("请提供货物量,单位:" + dispatchOrderNoteDetail.getMeasureUnit());
        }
        if (dispatchOrderNoteDetail.getGrossWeight() == null || dispatchOrderNoteDetail.getGrossWeight() < 0) {
            throw new BusinessException("请提供或估值货物毛重");
        }
        if (dispatchOrder.getPriceMode() == GoodsOrder.PriceMode.unitPrice &&
                (dispatchOrderNoteDetail.getGoodsUnitPrice() == null || dispatchOrderNoteDetail.getGoodsUnitPrice() <= 0)) {
            throw new BusinessException("单价计价模式下，请填写货物运输单价");
        }
    }
}
